<template>
  <!-- 排班类型 -->
  <section>
    <el-form :inline="true" @submit.native.prevent="getData(-1)">
      <el-form-item>
        <deptSearch v-model="form.deptId"></deptSearch>
      </el-form-item>
      <el-form-item>
        <el-select placeholder="状态" v-model="form.status" clearable>
          <el-option :value="1" label="正常"></el-option>
          <el-option :value="0" label="禁用"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" circle icon="el-icon-search" native-type="submit"></el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="openEdit(null)">+添加</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="$router.go(-1)">
          <i class="el-icon-arrow-left"></i> 返回
        </el-button>
      </el-form-item>
    </el-form>
    <table class="table-box">
      <thead>
        <tr>
          <th>名称</th>
          <th>部门</th>
          <th>类型</th>
          <th>时间</th>
          <th>启用状态</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in tableList" :key="index">
          <td>{{item.title}}</td>
          <td>{{item.deptName}}</td>
          <td>{{item.type==1?"门诊":"住院"}}</td>
          <td>
            <div style="margin-bottom:5px;" v-for="(item,index) in item.times" :key="index">{{item.startTime}}~{{item.endTime}}&nbsp;&nbsp;&nbsp;号源：{{item.sourceNumber==0?"无限制":item.sourceNumber}}</div>
          </td>
          <td>
            <span @click="onSwitch(item)">
              <el-switch :value="item.status==1"></el-switch>
            </span>
          </td>
          <td>
            <el-link type="primary" :underline="false" @click="openEdit(item)">查看</el-link>
          </td>
        </tr>
      </tbody>
    </table>
    <div v-if="tableList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
      <i class="iconfont iconwushuju" style="font-size:40px;"></i>
      <div>暂无数据</div>
    </div>
    <br />
    <pager v-model="pager" @change-pagenum="getData"></pager>
    <edit v-model="dialogEdit" :curItem="curItem" @success="getData"></edit>
  </section>
</template>
<script>
export default {
  components: {
    edit: () => import("./_com/editPaiBanType.vue"),
    "deptSearch": () => import("@/pages/_com/deptSearch.vue")
  },
  data() {
    return {
      dialogEdit: false,
      curItem: null,
      form: {
        hpId: "",
        deptId: "",
        status: 1,
        type: "" //1：门诊 2：住院
      },
      pager: { pageSize: 15, pageNum: 1, total: 0 },
      tableList: [],
      qxBtns: {},
      deptArray: []
    };
  },
  mounted() {
    let th = this;
    th.form.hpId = th.adminUser.hpId;
    th.getQX();
    th.getDept();
    th.getData();
  },
  methods: {
    onSwitch(obj) {
      const th = this;
      let j = {
        typeId: obj.id,
        type: 1,
        hpId: th.form.hpId,
        status: obj.status == 1 ? 0 : 1
      };
      th.showLoading();
      th.$api
        .updateScheduleType(j)
        .then(res => {
          th.showLoading(false);
          if (res.executed) {
            th.$message.success("操作成功");
            obj.status = j.status;
          } else {
            th.$message.error(res.message);
          }
        })
        .catch(res => {
          th.showLoading(false);
        });
    },
    openEdit(obj) {
      this.curItem = obj;
      this.dialogEdit = true;
    },
    //获取科室
    getDept(name) {
      let th = this;
      let j = {
        pageNum: 1,
        pageSize: 10,
        hpId: th.adminUser.hpId,
        deptName: name
      };
      th.$api.getDepartmentPageList(j).then(res => {
        if (res.executed) {
          th.deptArray = res.dataBody.list;
        }
      });
    },
    getData(type) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      let j = {};
      Object.assign(j, th.pager);
      Object.assign(j, th.form);
      th.showLoading();
      th.$api.getScheduleTypeList(j).then(res => {
        if (res.executed) {
          th.tableList = res.dataBody.list;
          th.pager.total = res.dataBody.total;
        }
        th.showLoading(false);
      });
    }
  }
};
</script>
